package com.hly.tool.object;


import com.hly.tool.attribute.Attribute;
import com.hly.tool.constants.HConstants;
import com.matrixone.apps.domain.DomainConstants;
import com.matrixone.apps.domain.DomainObject;
import com.matrixone.apps.domain.util.FrameworkException;
import com.matrixone.apps.domain.util.MapList;
import com.matrixone.apps.domain.util.MqlUtil;
import com.matrixone.apps.framework.ui.UIUtil;
import matrix.db.Context;
import matrix.util.MatrixException;
import matrix.util.StringList;

import java.util.*;

/**
 * @Auther: Jack.Hao
 * @Date: 2021/5/19 13:32
 * @Description:
 */
public class HDomainObject extends HConstants

{


    /**
     * 获取对象的基础属性
     *
     * @param context
     * @param objectId
     * @return 返回数据集合
     * @throws Exception
     */
    public static Map getKyBasics(Context context, String objectId) throws Exception
    {
        return getKyBasics(context, DomainObject.newInstance(context, objectId));
    }

    /**
     * 获取对象的基础属性
     *
     * @param context
     * @param domainObject
     * @return 返回数据集合
     * @throws Exception
     */
    public static Map getKyBasics(Context context, DomainObject domainObject) throws Exception
    {
        Map basicMap = new HashMap();
        try
        {
            basicMap = domainObject.getInfo(context, busList);

        } catch (MatrixException e)
        {
            e.printStackTrace();
        }
        return basicMap;
    }


    /**
     * 通过type name 查询对象集合
     *
     * @param context
     * @param type
     * @param name
     * @return 返回数据集合
     * @throws Exception
     */
    public static MapList findObjectByName(Context context, String type, String name) throws Exception
    {
        String whereStr = "name==\"" + name + "\"";
        MapList objectList = DomainObject.findObjects(context, type, "*", whereStr, busList);
        return objectList;
    }


    /**
     * 通过type name  revision查询对象集合
     *
     * @param context
     * @param type
     * @param name
     * @return 返回数据集合
     * @throws Exception
     */
    public static MapList findObjectByTNR(Context context, String type, String name, String revision) throws Exception
    {
        String whereStr = "name==\"" + name + "\" && revision ==\"" + revision + "\"";
        MapList objectList = DomainObject.findObjects(context, type, "*", whereStr, busList);
        return objectList;
    }

    /**
     * 通过type name  revision查询对象集合
     *
     * @param context
     * @param type
     * @param whereStr
     * @return 返回数据集合
     * @throws Exception
     */
    public static MapList findObjectByWhere(Context context, String type, String whereStr) throws Exception
    {
        return findObjectByWhere(context, type, "*", whereStr);
    }

    /**
     * 通过type name  revision查询对象集合
     *
     * @param context
     * @param type
     * @param whereStr
     * @return 返回数据集合
     * @throws Exception
     */
    public static MapList findObjectByWhere(Context context, String type, String vault, String whereStr) throws Exception
    {
        MapList objectList = DomainObject.findObjects(context, type, vault, whereStr, busList);
        return objectList;
    }


    /**
     * 通过当前对象、关联关系、子对象的Name 获取到子对象的 basic集合 和 关系属性的集合
     *
     * @param context
     * @param parentId 父节点id
     * @param nodeName 获取节点的名称
     * @return 返回数据集合
     * @throws Exception
     */
    public static MapList getChildrenBasicByName(Context context, String parentId, String nodeName, String relName, String childrenType) throws Exception
    {
        return getChildrenBasicByName(context, parentId, nodeName, relName, childrenType, null, null);
    }

    /**
     * 通过当前对象、关联关系、子对象的Name、对象指定属性的集合、关系的指定属性的集合 获取到子对象的属性及关系属性集合
     *
     * @param context
     * @param parentId 父节点id
     * @param nodeName 获取节点的名称
     * @return 数据集合
     * @throws Exception
     */
    public static MapList getChildrenBasicByName(Context context, String parentId, String nodeName, String relName, String childrenType, StringList selectbusList, StringList selectRelList) throws Exception
    {
        //父节点
        DomainObject parentNode = DomainObject.newInstance(context, parentId);
//        if (selectbusList != null && !selectbusList.isEmpty())
//        {
//            selectbusList.addAll(busList);
//            Set<String> busSet = new HashSet<String>(selectbusList);
//            selectbusList = new StringList(new ArrayList<String>(busSet));
//        }
//        if (selectRelList != null && !selectRelList.isEmpty())
//        {
//            selectRelList.addAll(relList);
//            Set<String> relSet = new HashSet<String>(selectRelList);
//            selectRelList = new StringList(new ArrayList<String>(relSet));
//        }
        String strWhere = "name=='" + nodeName + "'";
        MapList childrenNode = parentNode.getRelatedObjects(context, relName, childrenType, selectbusList, selectRelList, false, true, (short) 1, strWhere, null, 0);
        return childrenNode;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId    当前对象的id
     * @param relatedTye  关联对象的类型
     * @param relatedName 关联对象的Name
     * @param relatedName 关联对象的关系
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedBasicByName(Context context, String objectId, String relatedTye, String relatedName, String relName) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

        String strWhere = "name=='" + relatedName + "'";
        MapList relatedObjects = objectNode.getRelatedObjects(context, relName, relatedTye, busList, relList, false, true, (short) 1, strWhere, null, 0);
        return relatedObjects;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId      当前对象的id
     * @param relatedTye    关联对象的类型
     * @param relatedName   关联对象的Name
     * @param relatedName   关联对象的关系
     * @param selectbusList 要查询关联对象的属性集合
     * @param selectRelList 要查询关联关系的属性集合
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsByName(Context context, String objectId, String relatedTye, String relatedName, String relName, StringList selectbusList, StringList selectRelList) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

//        if (selectbusList != null && !selectbusList.isEmpty())
//        {
//            selectbusList.addAll(busList);
//            Set<String> busSet = new HashSet<String>(selectbusList);
//            selectbusList = new StringList(new ArrayList<String>(busSet));
//        }
//        if (selectRelList != null && !selectRelList.isEmpty())
//        {
//            selectRelList.addAll(relList);
//            Set<String> relSet = new HashSet<String>(selectRelList);
//            selectRelList = new StringList(new ArrayList<String>(relSet));
//        }

        String strWhere = "name=='" + relatedName + "'";
        MapList relatedObjects = objectNode.getRelatedObjects(context, relName, relatedTye, selectbusList, selectRelList, false, true, (short) 1, strWhere, null);
        return relatedObjects;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId      当前对象的id
     * @param relatedTye    关联对象的类型
     * @param relatedName   关联对象的Name
     * @param relName       关联对象的关系
     * @param selectbusList 要查询关联对象的属性集合
     * @param selectRelList 要查询关联关系的属性集合
     * @param form          关系的方向 true | false
     * @param to            关系的方向 true | false
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsByName(Context context, String objectId, String relatedTye, String relatedName, String relName, StringList selectbusList, StringList selectRelList, boolean form, boolean to) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

//        if (selectbusList != null && !selectbusList.isEmpty())
//        {
//            selectbusList.addAll(busList);
//            Set<String> busSet = new HashSet<String>(selectbusList);
//            selectbusList = new StringList(new ArrayList<String>(busSet));
//        }
//        if (selectRelList != null && !selectRelList.isEmpty())
//        {
//            selectRelList.addAll(relList);
//            Set<String> relSet = new HashSet<String>(selectRelList);
//            selectRelList = new StringList(new ArrayList<String>(relSet));
//        }

        String strWhere = "name=='" + relatedName + "'";
        MapList relatedObjects = objectNode.getRelatedObjects(context, relName, relatedTye, selectbusList, selectRelList, form, to, (short) 1, strWhere, null);
        return relatedObjects;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId    当前对象的id
     * @param relatedTye  关联对象的类型
     * @param relatedName 关联对象的Name
     * @param relName     关联对象的关系
     * @param form        关系的方向 true | false
     * @param to          关系的方向 true | false
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectAttrMapByName(Context context, String objectId, String relatedTye, String relatedName, String relName, boolean form, boolean to) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);
        String strWhere = "name=='" + relatedName + "'";

        StringList busList = getTypeBusList(context, relatedTye);

        MapList result = objectNode.getRelatedObjects(context, relName, relatedTye, busList, relList, form, to, (short) 1, strWhere, null, 0);

        return result;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId   当前对象的id
     * @param relatedTye 关联对象的类型
     * @param relName    关联对象的关系
     * @param form       关系的方向 true | false
     * @param to         关系的方向 true | false
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjects(Context context, String objectId, String relatedTye, String relName, boolean form, boolean to, short level) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

        MapList relatedObjects = objectNode.getRelatedObjects(context, relName, relatedTye, busList, relList, form, to, level, null, null, 0);

        return relatedObjects;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param obj        当前对象的id
     * @param relatedTye 关联对象的类型
     * @param relName    关联对象的关系
     * @param form       关系的方向 true | false
     * @param to         关系的方向 true | false
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjects(Context context, DomainObject obj, String relatedTye, String relName, boolean form, boolean to, short level) throws Exception
    {
        MapList relatedObjects = obj.getRelatedObjects(context, relName, relatedTye, busList, relList, form, to, level, null, null, 0);
        return relatedObjects;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId      当前对象的id
     * @param relatedTye    关联对象的类型
     * @param relName       关联对象的关系
     * @param selectbusList 对象属性集合
     * @param selectRelList 关系属性集合集合
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsAttr(Context context, String objectId, String relatedTye, String relName, StringList selectbusList, StringList selectRelList, short level) throws Exception
    {
        return getRelatedObjectsAttr(context, objectId, relatedTye, relName, selectbusList, selectRelList, false, true, level);
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId      当前对象的id
     * @param relatedTye    关联对象的类型
     * @param relName       关联对象的关系
     * @param selectbusList 对象属性集合
     * @param selectRelList 关系属性集合集合
     * @param form          关系的方向 true | false
     * @param to            关系的方向 true | false
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsAttr(Context context, String objectId, String relatedTye, String relName, StringList selectbusList, StringList selectRelList, boolean form, boolean to, short level) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

//        if (selectbusList != null && !selectbusList.isEmpty())
//        {
//            selectbusList.addAll(busList);
//            Set<String> busSet = new HashSet<String>(selectbusList);
//            selectbusList = new StringList(new ArrayList<String>(busSet));
//        }
//        if (selectRelList != null && !selectRelList.isEmpty())
//        {
//            selectRelList.addAll(relList);
//            Set<String> relSet = new HashSet<String>(selectRelList);
//            selectRelList = new StringList(new ArrayList<String>(relSet));
//        }


        MapList relatedObjects = objectNode.getRelatedObjects(context, relName, relatedTye, selectbusList, selectRelList, form, to, level, null, null, 0);

        return relatedObjects;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param domainObject  当前对象
     * @param relatedTye    关联对象的类型
     * @param relName       关联对象的关系
     * @param selectbusList 对象属性集合
     * @param selectRelList 关系属性集合集合
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsAttr(Context context, DomainObject domainObject, String relatedTye, String relName, StringList selectbusList, StringList selectRelList, short level) throws Exception
    {
        return getRelatedObjectsAttr(context, domainObject, relatedTye, relName, selectbusList, selectRelList, false, true, level);
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param domainObject  当前对象
     * @param relatedTye    关联对象的类型
     * @param relName       关联对象的关系
     * @param selectbusList 对象属性集合
     * @param selectRelList 关系属性集合集合
     * @param form          关系的方向 true | false
     * @param to            关系的方向 true | false
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsAttr(Context context, DomainObject domainObject, String relatedTye, String relName, StringList selectbusList, StringList selectRelList, boolean form, boolean to, short level) throws Exception
    {
//        if (selectbusList != null && !selectbusList.isEmpty())
//        {
//            selectbusList.addAll(busList);
//            Set<String> busSet = new HashSet<String>(selectbusList);
//            selectbusList = new StringList(new ArrayList<String>(busSet));
//        }
//        if (selectRelList != null && !selectRelList.isEmpty())
//        {
//            selectRelList.addAll(relList);
//            Set<String> relSet = new HashSet<String>(selectRelList);
//            selectRelList = new StringList(new ArrayList<String>(relSet));
//        }


        MapList relatedObjects = domainObject.getRelatedObjects(context, relName, relatedTye, selectbusList, selectRelList, form, to, level, null, null, 0);

        return relatedObjects;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param domainObject 当前对象
     * @param relatedTye   关联对象的类型
     * @param relName      关联对象的关系
     * @param form         关系的方向 true | false
     * @param to           关系的方向 true | false
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsAttrMap(Context context, DomainObject domainObject, String relatedTye, String relName, boolean form, boolean to, short level) throws Exception
    {
        return getRelatedObjectsAttr(context, domainObject, relatedTye, relName, busList, relList, false, true, (short) 0);
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId   当前对象的id
     * @param relatedTye 关联对象的类型
     * @param relName    关联对象的关系
     * @param form       关系的方向 true | false
     * @param to         关系的方向 true | false
     * @param busWhere   对象的条件
     * @param relWhere   关系的条件
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjects(Context context, String objectId, String relatedTye, String relName, boolean form, boolean to, short level, String busWhere, String relWhere) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

        StringList busList = getTypeBusList(context, relatedTye);

        MapList result = objectNode.getRelatedObjects(context, relName, relatedTye, busList, relList, form, to, level, busWhere, relWhere, 0);

        return result;
    }


    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param objectId   当前对象的id
     * @param relatedTye 关联对象的类型
     * @param relName    关联对象的关系
     * @param form       关系的方向 true | false
     * @param to         关系的方向 true | false
     * @param busWhere   对象的条件
     * @param relWhere   关系的条件
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsAttr(Context context, String objectId, String relatedTye, String relName, StringList selectbusList, StringList selectRelList, boolean form, boolean to, short level, String busWhere, String relWhere) throws Exception
    {
        //父节点
        DomainObject obj = DomainObject.newInstance(context, objectId);

        return getRelatedObjectsAttr( context,  obj,  relatedTye,  relName,  selectbusList,  selectRelList,  form,  to,  level,  busWhere,  relWhere);
    }

    /**
     * 通过关联关系获取 关系的另一端的对象，通过属性或basic值
     *
     * @param context
     * @param domainObject 当前对象
     * @param relatedTye   关联对象的类型
     * @param relName      关联对象的关系
     * @param form         关系的方向 true | false
     * @param to           关系的方向 true | false
     * @param busWhere     对象的条件
     * @param relWhere     关系的条件
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedObjectsAttr(Context context, DomainObject domainObject, String relatedTye, String relName, StringList selectbusList, StringList selectRelList, boolean form, boolean to, short level, String busWhere, String relWhere) throws Exception
    {

        MapList relatedObjects = domainObject.getRelatedObjects(context, relName, relatedTye, selectbusList, selectRelList, form, to, level, busWhere, relWhere, 0);

        return relatedObjects;
    }


    /**
     * 检查当前对象关联对象的属性值
     *
     * @param context      上下文
     * @param objectId     当前对象的id
     * @param relatedType  关联对象的类型
     * @param relationship 与关联对象的关系
     * @param attr         关联对象的属性名称
     * @param attrValue    关联对象的属性值
     * @return
     * @throws Exception
     */
    public boolean checkRelatedObjectAttr(Context context, String objectId, String attr, String attrValue, String relatedType, String relationship) throws Exception
    {
        DomainObject domainObject = DomainObject.newInstance(context, objectId);
        return checkRelatedObjectAttr(context, domainObject, attr, attrValue, relatedType, relationship);
    }


    /**
     * 检查当前对象关联对象的属性值
     * 如果关联对象的属性值 和 检查值相同 返回true 否则 返回false
     *
     * @param context      上下文
     * @param domainObject 当前对象
     * @param relatedType  关联对象的类型
     * @param relationship 与关联对象的关系
     * @param attr         关联对象的属性名称
     * @param attrValue    关联对象的属性值
     * @return
     * @throws Exception
     */
    public boolean checkRelatedObjectAttr(Context context, DomainObject domainObject, String attr, String attrValue, String relatedType, String relationship) throws Exception
    {
        return checkRelatedObjectAttr(context, domainObject, relatedType, relationship, attr, attrValue, false, true);
    }


    /**
     * 检查当前对象关联对象的属性值
     * 如果关联对象的属性值 和 检查值相同 返回true 否则 返回false
     *
     * @param context      上下文
     * @param objectId     当前对象的id
     * @param relatedType  关联对象的类型
     * @param relationship 与关联对象的关系
     * @param attr         关联对象的属性名称
     * @param attrValue    关联对象的属性值
     * @param from         与关联对象的关系的方向
     * @param to           与关联对象的关系的方向
     * @return
     * @throws Exception
     */
    public boolean checkRelatedObjectAttr(Context context, String objectId, String relatedType, String relationship, String attr, String attrValue, boolean from, boolean to) throws Exception
    {
        DomainObject domainObject = DomainObject.newInstance(context, objectId);
        return checkRelatedObjectAttr(context, domainObject, relatedType, relationship, attr, attrValue, from, to);
    }


    /**
     * 检查当前对象关联对象的属性值
     * 如果关联对象的属性值 和 检查值相同 返回true 否则 返回false
     *
     * @param context      上下文
     * @param domainObject 当前对象
     * @param relatedType  关联对象的类型
     * @param relationship 与关联对象的关系
     * @param attr         关联对象的属性名称
     * @param attrValue    关联对象的属性值
     * @param from         与关联对象的关系的方向
     * @param to           与关联对象的关系的方向
     * @return
     * @throws Exception
     */
    public boolean checkRelatedObjectAttr(Context context, DomainObject domainObject, String relatedType, String relationship, String attr, String attrValue, boolean from, boolean to) throws Exception
    {
        StringList busList = new StringList();
        StringList relList = new StringList();
        busList.add(DomainConstants.SELECT_ID);
        busList.add("attribute[" + attr + "]");
        MapList relatedRoutes = domainObject.getRelatedObjects(context, relationship, relatedType, busList, relList, from, to, (short) 1, null, null, 0);

        for (int i = 0; i < relatedRoutes.size(); i++)
        {
            Map map_i = (Map) relatedRoutes.get(i);
            String attrVal_i = (String) map_i.get("attribute[" + attr + "]");
            if (UIUtil.isNotNullAndNotEmpty(attrVal_i) && attrVal_i.equals(attrValue))
            {
                return true;
            }
        }
        return false;
    }


    /**
     * 通过关联关系获取 指定层级的对象
     *
     * @param context
     * @param objectId   当前对象的id
     * @param relatedTye 关联对象的类型
     * @param relName    关联对象的关系
     * @param form       关系的方向 true | false
     * @param to         关系的方向 true | false
     * @param busWhere   对象的条件
     * @param relWhere   关系的条件
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedLevelObjects(Context context, String objectId, String relatedTye, String relName, boolean form, boolean to, short level, String busWhere, String relWhere) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

        StringList busList = getTypeBusList(context, relatedTye);

        MapList relatedObjects = objectNode.getRelatedObjects(context, relName, relatedTye, busList, relList, form, to, level, busWhere, relWhere, 0);

        MapList result = new MapList();
        for (int i = 0; i < relatedObjects.size(); i++)
        {
            Map map_i = (Map) relatedObjects.get(i);
            String level_i = (String) map_i.get(DomainConstants.SELECT_LEVEL);
            if (Short.valueOf(level_i) == level)
            {
                result.add(map_i);
            }
            result.add(map_i);
        }

        return result;
    }


    /**
     * 通过关联关系获取 指定层级的对象的属性
     *
     * @param context
     * @param objectId   当前对象的id
     * @param relatedTye 关联对象的类型
     * @param relName    关联对象的关系
     * @param busList    对象属性集合
     * @param relList    关联属性集合
     * @param form       关系的方向 true | false
     * @param to         关系的方向 true | false
     * @param level      层级
     * @param busWhere   对象的条件
     * @param relWhere   关系的条件
     * @return
     * @throws Exception
     */
    public static MapList getRelatedLevelObjectsAttr(Context context, String objectId, String relatedTye, String relName, StringList busList, StringList relList, boolean form, boolean to, short level, String busWhere, String relWhere) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

        MapList relatedObjects = objectNode.getRelatedObjects(context, relName, relatedTye, busList, relList, form, to, level, busWhere, relWhere);

        MapList result = new MapList();
        for (int i = 0; i < relatedObjects.size(); i++)
        {
            Map map_i = (Map) relatedObjects.get(i);
            String level_i = (String) map_i.get(DomainConstants.SELECT_LEVEL);
            if (Short.valueOf(level_i) == level)
            {
                result.add(map_i);
            }
            result.add(map_i);
        }

        return result;
    }


    /**
     * 获取对象的所有属性值
     *
     * @param context
     * @param objectId
     * @return
     * @throws Exception
     */
    public static Map getAllAttrMap(Context context, String objectId) throws Exception
    {
        Map basicMap = HDomainObject.getKyBasics(context, objectId);
        Map attrMap = Attribute.getObjAttributeMap(context, objectId);
        attrMap.putAll(basicMap);
        return attrMap;
    }

    /**
     * 获取对象的所有属性值
     *
     * @param context
     * @param domainObject
     * @return
     * @throws Exception
     */
    public static Map getAllAttrMap(Context context, DomainObject domainObject) throws Exception
    {
        Map basicMap = HDomainObject.getKyBasics(context, domainObject);
        Map attrMap = Attribute.getObjAttributeMap(context, domainObject);
        attrMap.putAll(basicMap);
        return attrMap;
    }


    /**
     * 通过关联关系获取 指定层级指定属性值的对象
     *
     * @param context
     * @param objectId   当前对象的id
     * @param relatedTye 关联对象的类型
     * @param relName    关联对象的关系
     * @param form       关系的方向 true | false
     * @param to         关系的方向 true | false
     * @param busWhere   对象的条件
     * @param relWhere   关系的条件
     * @return 关联对象的集合
     * @throws Exception
     */
    public static MapList getRelatedLevelAttrValueObjects(Context context, String objectId, String relatedTye, String relName, boolean form, boolean to, short level, String busWhere, String relWhere, String attr, String attrValue) throws Exception
    {
        //父节点
        DomainObject objectNode = DomainObject.newInstance(context, objectId);

        StringList busList = getTypeBusList(context, relatedTye);

        if (!busList.contains("attribute[" + attr + "]"))
        {
            busList.add("attribute[" + attr + "]");
        }

        MapList relatedObjects = objectNode.getRelatedObjects(context, relName, relatedTye, busList, relList, form, to, level, busWhere, relWhere, 0);

        MapList result = new MapList();
        for (int i = 0; i < relatedObjects.size(); i++)
        {
            Map map_i = (Map) relatedObjects.get(i);
            String level_i = (String) map_i.get(DomainConstants.SELECT_LEVEL);
            String attrValue_i = (String) map_i.get("attribute[" + attr + "]");
            if (level_i.equals(String.valueOf(level)) && attrValue_i.equals(attrValue))
            {
                result.add(map_i);
            }
        }
        return result;
    }


    /**
     * 获取父节点 map from[false] to[true]
     *
     * @param mapList
     * @param map
     * @return
     */
    public Map getParentBySelectTo(MapList mapList, Map map)
    {
        Map parentMap = new HashMap();
        String parentId = (String) map.get("from.id");
        for (int i = 0; i < mapList.size(); i++)
        {
            Map map_i = (Map) mapList.get(i);
            String id_i = (String) map_i.get(DomainConstants.SELECT_ID);
            if (id_i.equals(parentId))
            {
                parentMap = map_i;
                break;
            }
        }

        return parentMap;
    }

    /**
     * 获取父节点 map from[true] to[false]
     *
     * @param mapList
     * @param map
     * @return
     */
    public Map getParentBySelectFrom(MapList mapList, Map map)
    {
        Map parentMap = new HashMap();

        String objectId = (String) map.get(DomainConstants.SELECT_ID);
        for (int i = 0; i < mapList.size(); i++)
        {
            Map map_i = (Map) mapList.get(i);
            String toId_i = (String) map_i.get(DomainConstants.SELECT_TO_ID);
            if (toId_i.equals(objectId))
            {
                parentMap = map_i;
                break;
            }
        }

        return parentMap;
    }


    /**
     * 获取父节点 map from[false] to[true]
     *
     * @param mapList
     * @param map
     * @return
     */
    public Map getChildrenBySelectTo(MapList mapList, Map map)
    {
        Map parentMap = new HashMap();
        String parentId = (String) map.get("from.id");
        for (int i = 0; i < mapList.size(); i++)
        {
            Map map_i = (Map) mapList.get(i);
            String id_i = (String) map_i.get(DomainConstants.SELECT_ID);
            if (id_i.equals(parentId))
            {
                parentMap = map_i;
                break;
            }
        }

        return parentMap;
    }

    /**
     * 获取父节点 map from[true] to[false]
     *
     * @param mapList
     * @param map
     * @return
     */
    public Map getParentMapBySelectFrom(MapList mapList, Map map)
    {
        Map parentMap = new HashMap();

        String objectId = (String) map.get(DomainConstants.SELECT_ID);
        for (int i = 0; i < mapList.size(); i++)
        {
            Map map_i = (Map) mapList.get(i);
            String toId_i = (String) map_i.get(DomainConstants.SELECT_TO_ID);
            if (toId_i.equals(objectId))
            {
                parentMap = map_i;
                break;
            }
        }

        return parentMap;
    }


    /**
     * 获取 一个 type 的所有属性 和基础属性的集合
     *
     * @param context
     * @param type
     * @return
     * @throws FrameworkException
     */
    public static StringList getTypeBusList(Context context, String type) throws FrameworkException
    {
        StringList busList = new StringList(HConstants.basicList);
        String typeMql = "print type $1 select attribute dump |";
        String typeMqlResult = MqlUtil.mqlCommand(context, typeMql, type);

        if (UIUtil.isNotNullAndNotEmpty(typeMqlResult))
        {
            String[] split = typeMqlResult.split("\\|");
            for (String attr : split)
            {
                busList.add("attribute[" + attr + "]");
            }
        }
        return busList;
    }


}
